﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PaiKe.Core.FileDataBase
{
    /// <summary>
    /// 文件部分
    /// </summary>
    public class PackFile
    {
        private FileStream _sourceFile;
        private BinaryReader _br;
        private long _contentStartPos;
        private int _fileCount;
        private List<long> _fileLengthList = new List<long>();
        private List<string> _shortNameList = new List<string>();


        internal PackFile(FileStream srcFile, BinaryReader br)
        {
            this._sourceFile = srcFile;
            _br = br;
            this._fileCount = _br.ReadInt32();//取文件数

            for (int i = 1; i <= _fileCount; i++)
            {
                this._fileLengthList.Add(_br.ReadInt64());
            }

            for (int i = 1; i <= _fileCount; i++)
            {
                this._shortNameList.Add(_br.ReadString());
            }
            this._contentStartPos = _sourceFile.Position;//设置实体文件内容的起始位置

        }


        public MemoryStream GetStream(int index)
        {
            return new MemoryStream(GetBytes(index));
        }

        public byte[] GetBytes(int index)
        {
            long startPos = this._contentStartPos;

            for (int i = 0; i < index; i++)
            {
                startPos += this._fileLengthList[i];
            }

            _sourceFile.Position = startPos;
            return _br.ReadBytes((int)_fileLengthList[index]);
        }

        public void OutputAllToDirectory(string dir)
        {
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            for (int i = 0; i < _fileCount; i++)
            {
                File.WriteAllBytes(dir + this._shortNameList[i], GetBytes(i));
            }
        }

        /// <summary>
        /// 以新名字输出某个内容到文件
        /// </summary>
        /// <param name="index"></param>
        /// <param name="file"></param>
        public void OutputOneToFile(int index, string file)
        {
            File.WriteAllBytes(file, GetBytes(index));
        }

        /// <summary>
        /// 用原始文件名输出某个内容到指定文件夹
        /// </summary>
        /// <param name="index"></param>
        /// <param name="dir"></param>
        public void OutputOneToDirectory(int index, string dir)
        {
            string name = _shortNameList[index];
            File.WriteAllBytes(Path.Combine(dir, name), GetBytes(index));
        }


        internal void Close()
        {
            if (_sourceFile != null)
            {
                _br.Close();
                _br = null;
                _sourceFile.Close();
                _sourceFile = null;
            }
        }
        #region 属性

        //源包文件
        public string CurrentPackFile
        {
            get
            {
                return this._sourceFile.Name;
            }
        }

        public int FileCount
        {
            get
            {
                return this._fileCount;
            }
        }


        public string[] NameList
        {
            get
            {
                return this._shortNameList.ToArray();
            }
        }
        #endregion
    }
}
